package com.iotek.bookstore.web.controlle;

import com.iotek.bookstore.modle.entity.Book;
import com.iotek.bookstore.modle.entity.BookSize;
import com.iotek.bookstore.modle.entity.TitleTwo;
import com.iotek.bookstore.modle.service.serviceimpl.BookBizImpl;
import com.iotek.bookstore.modle.service.serviceimpl.TitleTwoBizImpl;
import net.sf.json.JSON;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/login/booksClassServlet")
public class BooksClassServlet extends CommonServlet {
    @Override
    public void doInsert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    public void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    public void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String type=req.getParameter("type");
        if(type.equals("dataInit")){
            doDataInit(req,resp);
        }else if(type.equals("ajax")){
            doAjaxData(req,resp);
        }else if(type.equals("paging")){
            doPagingtQuery(req,resp);
        }
        if(req.getParameter("sortType")!=null){
            doSortQuery(req,resp);
        }
    }

    //初始化图书分类数据
    public void doDataInit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int titleOneId=Integer.parseInt(req.getParameter("titleOneId"));
        int titleTwoId=Integer.parseInt(req.getParameter("titleTwoId"));
        List<TitleTwo>titleTwos=new TitleTwoBizImpl().query(titleOneId);
        int gross=0;//保存一级分类下书的总数
        List<BookSize>bookSizes=new ArrayList<BookSize>();
        for(int i=0;i<titleTwos.size();i++){
            int id=titleTwos.get(i).getId();
            int number=new BookBizImpl().query(id).size();
            gross+=number;
            BookSize bookSize=new BookSize(id,number);
            bookSizes.add(bookSize);
        }

        //表示当前种类书籍的数量
        int size=new BookBizImpl().query(titleTwoId).size();
        int pages;//当前种类书籍在页面上显示的页数
        if(size%2==0){
            pages=size/2;
        }else {
            pages=size/2+1;
        }
        List<Book>books=new BookBizImpl().query(titleTwoId,0,2,"synthesize");
        for (Book book:books){
            System.out.println("书名："+book.getBook());
        }
        req.setAttribute("gross",gross);
        req.setAttribute("books",books);
        req.setAttribute("titleTwos",titleTwos);
        req.setAttribute("bookSizes",bookSizes);
        req.setAttribute("pages",pages);
        req.getSession().setAttribute("titleTwoId",titleTwoId);
        req.getRequestDispatcher("/booksClass.jsp").forward(req,resp);

    }

    //初始化分类页面数据
    public void doAjaxData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int titleTwoId=Integer.parseInt(req.getParameter("titleTwoId"));
        String flag=req.getParameter("flag");
        //表示当前种类书籍的数量
        int size=new BookBizImpl().query(titleTwoId).size();
        int pages;//当前种类书籍在页面上显示的页数
        if(size%2==0){
            pages=size/2;
        }else {
            pages=size/2+1;
        }
        List<Book>books=new BookBizImpl().query(titleTwoId,0,2,"synthesize");
        JSON json=JSONArray.fromObject(books);
        System.out.println(json.toString());
        if(flag.equals("pages")){
            resp.getWriter().write(String.valueOf(pages));
        }else if(flag.equals("data")){
            resp.getWriter().write(json.toString());
        }
        String sortType="synthesize";
        //保存二级分类ID
        req.getSession().setAttribute("titleTwoId",titleTwoId);
        //保存排序方式
        req.getSession(false).setAttribute("sortType",sortType);
    }

    //排序查询书籍
    public void doSortQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int titleTwoId=(Integer) req.getSession().getAttribute("titleTwoId");
        String sortType=req.getParameter("sortType");
        req.getSession().setAttribute("sortType",sortType);
        List<Book>books=new BookBizImpl().query(titleTwoId,0,2,sortType);
        JSON json=JSONArray.fromObject(books);
        System.out.println(json.toString());
        resp.getWriter().write(json.toString());
    }

    //翻页查询功能
    public void doPagingtQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int titleTwoId=(Integer) req.getSession().getAttribute("titleTwoId");
        String sortType=(String) req.getSession().getAttribute("sortType");
        if(sortType==null){
            sortType="synthesize";
        }
        int start=Integer.parseInt(req.getParameter("start"));
        System.out.println("ID:"+titleTwoId+"----star:"+start+"---sort:"+sortType);
        List<Book>books=new BookBizImpl().query(titleTwoId,start,2,sortType);

        JSON json=JSONArray.fromObject(books);
        resp.getWriter().write(json.toString());
    }
}
